package discrete.math;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
/**
 * 
 * @author William Larson-Garcia
 * Creates a Matrix Panel with 3 Buttons and a Table displaying matrix values
 *
 */

public class MatrixPanel extends JPanel implements ActionListener{
		private MatrixMath matrix;
	    private javax.swing.JButton clear;
	    private javax.swing.JButton autoFill;
	    private javax.swing.JButton submit;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JTable matrixTable;

	public MatrixPanel(MatrixMath matrix) {
		this.matrix = matrix;

        clear = new javax.swing.JButton();
        autoFill= new javax.swing.JButton();
        submit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        matrixTable = new javax.swing.JTable(matrix.getMrows(), matrix.getMcolumns());

        jLabel1 = new javax.swing.JLabel();
        String[] columnNames = {"-","A", "B", "C","D","E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
				"P", "Q", "R", "S", "T"};

        clear.setText("Clear");

        autoFill.setText("AutoFill");

        submit.setText("Submit");
        
        
		clear.addActionListener(this);
        autoFill.addActionListener(this);
        submit.addActionListener(this);
        
        
        for( int j = 0; j < matrix.getMcolumns(); j++){
        	matrixTable.setValueAt(columnNames[j], 0, j);
		}
		for (int i = 0; i < matrix.getMrows(); i++){
			matrixTable.setValueAt(columnNames[i], i, 0);
		}

		for( int i = 1; i <  matrix.getMrows(); i++){			
			for( int j = 1; j < matrix.getMcolumns(); j++){	
				matrixTable.setValueAt(matrix.printSingleValue(i, j), i, j);				
			}			
		}

        
        jScrollPane1.setViewportView(matrixTable);

        jLabel1.setText("Matrix Panel");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(layout.createSequentialGroup()
                                .add(clear)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(autoFill)
                                .add(52, 52, 52)
                                .add(submit))
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(168, 168, 168)
                        .add(jLabel1)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(clear)
                    .add(autoFill)
                    .add(submit)
                //.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        )));
 }
		
	public MatrixPanel(boolean doubleBuffered, MatrixMath matrix) {
		this(matrix);
		this.setDoubleBuffered(doubleBuffered);		
	}

	        
	    	public void actionPerformed(ActionEvent e) {
	    		int val;
	    		int swapR;
	    		int swapC;
	    		
	    		if(e.getSource()==submit){
	    			
	    			for( int k = 1; k < matrix.getMrows(); k++){
	    				for( int l =1; l < matrix.getMcolumns(); l++){
	    					val = Integer.parseInt(matrixTable.getValueAt(k,l).toString());
	    					if(val == 1){
	    						if( k != l){
	    						swapR= l;
	    						swapC = k;
	    						matrix.manualFill(k, l, val);
	    						matrixTable.setValueAt(matrix.printSingleValue(swapR, swapC), swapR, swapC);				
	    					}
	    						else{
	    							matrix.manualFill(k, l, 0);
	    							matrixTable.setValueAt(matrix.printSingleValue(k, l), k, l);
	    						}
	    							
	    					}											
	    				
	    				}
	    			}
	    			
	    		}
	    		
	    		if(e.getSource()==autoFill){
	    		matrix.autoFill(matrix.getMrows(), matrix.getMcolumns());
	    			for( int i = 1; i <  matrix.getMrows(); i++){			
	    				for( int j = 1; j < matrix.getMcolumns(); j++){
	    					matrixTable.setValueAt(matrix.printSingleValue(i, j), i, j);				
	    				}			
	    			}	
	    		}
	    		
	    		if(e.getSource()==clear){
	    			matrix.clearMatrix();
	    			for( int i = 1; i <  matrix.getMrows(); i++){			
	    				for( int j = 1; j < matrix.getMcolumns(); j++){
	    					matrixTable.setValueAt(matrix.printSingleValue(i, j), i, j);				
	    				}			
	    			}
	    			
	    		}
	    		
	    	}
	            
	       
	}





